package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Date;
import java.util.Locale;

public class SavingMoneyActivity extends AppCompatActivity {
    private EditText incomeEditText;
    private EditText wasteEditText;
    private Button calculateButton;
    private Button historyButton;
    private Spinner incomeCategorySpinner;
    private Spinner wasteCategorySpinner;

    private String selectedIncomeCategory = "工资"; // 默认值
    private String selectedWasteCategory = "餐饮"; // 默认值

    private DatabaseHelper dbHelper;

    @SuppressLint({"WrongViewCast", "MissingInflatedId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saving_money);

        dbHelper = new DatabaseHelper(this);

        incomeEditText = findViewById(R.id.income);
        wasteEditText = findViewById(R.id.waste);
        calculateButton = findViewById(R.id.result);
        historyButton = findViewById(R.id.history);
        incomeCategorySpinner = findViewById(R.id.spinner_income_category);
        wasteCategorySpinner = findViewById(R.id.spinner_waste_category);

        // 设置收入分类选择监听
        incomeCategorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedIncomeCategory = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        wasteCategorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedWasteCategory = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateAndSave();
            }
        });

        historyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SavingMoneyActivity.this, HistoryActivity.class);
                startActivity(intent);
            }
        });
    }

    private void calculateAndSave() {
        String incomeText = incomeEditText.getText().toString();
        String wasteText = wasteEditText.getText().toString();

        if (incomeText.isEmpty() && wasteText.isEmpty()) {
            Toast.makeText(this, "请至少输入收入或消费金额", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            double income = incomeText.isEmpty() ? 0.0 : Double.parseDouble(incomeText);
            double waste = wasteText.isEmpty() ? 0.0 : Double.parseDouble(wasteText);

            double previousBalance = dbHelper.getLatestBalance();
            double newBalance = previousBalance + income - waste;

            String currentDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
            dbHelper.addRecord(
                    currentDate,
                    income,
                    selectedIncomeCategory,
                    waste,
                    selectedWasteCategory,
                    newBalance
            );

            Toast.makeText(this, "记录已保存", Toast.LENGTH_SHORT).show();

            incomeEditText.setText("");
            wasteEditText.setText("");
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
        }
    }
}